package com.jieyang.oa.controller.login;

import com.jieyang.oa.controller.BaseServlet;
import com.jieyang.oa.pojo.entity.Employee;
import com.jieyang.oa.service.EmailService;
import com.jieyang.oa.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.IOException;
import java.util.Random;

/**
 * @author 29276
 * @version 1.0
 * @description: TODO
 * @date 2021/8/28 15:33
 */
@WebServlet("/forgetPwdServlet")
public class ForgetPwdServlet extends BaseServlet {

    @Autowired
    private EmailService emailService;

    @Autowired
    private EmployeeService employeeService;

    public void emailCode(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setContentType("text/html;charset=utf-8");

        String empId = req.getParameter("empId");
        String email = req.getParameter("email");
        Employee employee = employeeService.selectEmpByEmpId(empId);
        if (email.equals(employee.getEmail())) {
            System.out.println("发验证码");
            StringBuilder sRand = new StringBuilder();
            Random random = new Random();
            for (int i = 0; i < 6; i++) {
                String rand = String.valueOf(random.nextInt(10));
                sRand.append(rand);
            }
            String content = "您的验证码是 " + sRand + " 。请在五分钟内验证！";
            String subject = "忘记密码";
            req.getSession().setAttribute("emailCode", sRand+"."+empId);
            emailService.sendSimpleMail(email, subject, content);
//            System.out.println(sRand);
            resp.getWriter().write("true");
        }
        else {
            resp.getWriter().write("false");
        }
    }
    public void emailCodeConfirm(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setContentType("text/html;charset=utf-8");
        String emailCode = req.getParameter("emailCode");
        String emailCodeSession = (String) req.getSession().getAttribute("emailCode");
        String[] split = emailCodeSession.split("\\.");
        if (emailCode.equals(split[0])) {
            resp.getWriter().write("true");
        }
        else {
            resp.getWriter().write("false");
        }
    }

    public void updatePwd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setContentType("text/html;charset=utf-8");
        String newPassword = req.getParameter("newPassword");
        String newPasswordSure = req.getParameter("newPasswordSure");

        if (newPassword.length()<8) {
            resp.getWriter().write("4");
        }
        else if (!newPassword.equals(newPasswordSure)) {
            resp.getWriter().write("3");
        }
        else {
            String emailCodeSession = (String) req.getSession().getAttribute("emailCode");
            String[] split = emailCodeSession.split("\\.");
            Employee emp = employeeService.selectEmpByEmpId(split[1]);
            emp.setPassword(newPassword);
            Integer update = employeeService.update(emp);
            if (update == 0) {
                resp.getWriter().write("0");
            } else {
                resp.getWriter().write("1");
            }
        }
    }

}
